package com.caocaog.market.data.transfer.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caocaog.market.data.transfer.entity.CcUser;
import com.caocaog.market.data.transfer.entity.MkSysUser;
import com.caocaog.market.data.transfer.mapper.MkSysUserMapper;
import com.caocaog.market.data.transfer.service.MkSysUserService;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author liguang
 * @version 1.0.0
 * @Project jeecg-boot
 * @Package com.caocaog.market.data.transfer.service.impl
 * @ClassName MkSysUserServiceImpl.java
 * @createTime 2021年11月16日 10:07:00
 * @Description TODO
 */
@Service
public class MkSysUserServiceImpl extends ServiceImpl<MkSysUserMapper, MkSysUser> implements MkSysUserService {
    @Override
    public IPage<MkSysUser> findAllSysUserByNow(Page<MkSysUser> page,MkSysUser mkSysUser) {
        return  super.getBaseMapper().findAllSysUserByNow(page,mkSysUser);
    }
    @Override
    public int countForfindAllSysUserByNow(MkSysUser mkSysUser) {
        return super.getBaseMapper().countForfindAllSysUserByNow(mkSysUser);
    }

    @Override
    public String insertSysUserForReplace(List<CcUser> ccUserList) {
       LoginUser currentUser =  (LoginUser) SecurityUtils.getSubject().getPrincipal();
        int insertSuc = 0,checkNum=0,updateNum=0;
        System.out.println("agentList------->  "+ccUserList.size());
        for (CcUser ccUser:ccUserList) {
            // siteId 需要从mk_site_basic中查询
            MkSysUser sysUser = new MkSysUser(ccUser.getLoginName(),ccUser.getName(),null,null,
                    ccUser.getSex()==1?1:2,ccUser.getMobile(),String.valueOf(ccUser.getAgentSid()),ccUser.getStatus()==0?1:2,0,2,null);
            int num = super.getBaseMapper().countForfindAllSysUserByNow(sysUser);
            // 先根据所有字段的值查询该条数据有没有或者是否改变；如果没有（即查询的值大于0）改变则不进行replace into操作
            if(num>0){
                System.out.println("库中已经存在的 用户名----》  "+sysUser.getUsername());
                checkNum=checkNum+1;
                // 分站已经存在，且没有经过更改，不再进行replace into 操作
                continue;
            }else{
                // 分站用户存在，但是经过修改，造成与“曹操来了”系统用户信息不一致，此时需要进行update操作
                MkSysUser queryUser = new MkSysUser();
                int countByUsername = super.getBaseMapper().countForfindAllSysUserByNow(queryUser.setUsername(ccUser.getLoginName()));
                if(countByUsername>0){
                    // 进行update操作
                    UpdateWrapper<MkSysUser> updateWrapper = new UpdateWrapper<MkSysUser>();
                    updateWrapper.set("sex",ccUser.getSex()==1?1:2);
                    updateWrapper.set("phone",ccUser.getMobile());
                    updateWrapper.set("status",ccUser.getStatus()==0?1:2);
                    updateWrapper.set("realname",ccUser.getName());
                    updateWrapper.set("update_by",currentUser.getUsername());
                    updateWrapper.set("update_time",new Date());
                    updateWrapper.eq("username",ccUser.getLoginName());
                    updateNum = updateNum + super.getBaseMapper().update(null,updateWrapper);
                    continue;
                }
                // 分站不存在,直接插入
                // 执行向mysql库中的sys_user表插入的逻辑
                sysUser.setId(String.valueOf(new DefaultIdentifierGenerator().nextId(sysUser)));
                sysUser.setCreateBy(currentUser.getUsername());
                sysUser.setCreateTime(new Date());
                // 处理密码
                String salt = oConvertUtils.randomGen(8);
                sysUser.setSalt(salt);
                sysUser.setPassword(PasswordUtil.encrypt(sysUser.getUsername(), "caocao123", salt));
                System.out.println("is----->   delete"+ccUser.getIsDelete());
                sysUser.setDelFlag(ccUser.getIsDelete());
                System.out.println("del----->   Flag"+sysUser.getDelFlag());
                int count = super.getBaseMapper().insertSysUserForReplace(sysUser);
                insertSuc = count>0 ? insertSuc+1 : insertSuc;
            }
        }
        return "总共执行成功"+(checkNum+insertSuc+updateNum)+"条数据；其中：新转移数据"+insertSuc+"条，新库中已存在且未经过修改的有"+checkNum+"条,“曹操来了”系统已存在但是用户被修改后再次同步过来的有"+updateNum+"条 。";
    }
}
